package cn.ym.mall.dao.impl;

import cn.ym.mall.dao.CommentDao;
import cn.ym.mall.entity.Comment;
import cn.ym.mall.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 评论dao实现类
 */
public class CommentDaoImpl implements CommentDao {
    QueryRunner queryRunner = new QueryRunner();

    /**
     * 通过商品查询评论
     *
     * @param gid
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public List<Comment> getCommentByGid(Integer gid, Integer pageNo, Integer pageSize) throws SQLException {
        List<Comment> commentList = new ArrayList<>();
        try {
            String sql = "select * from ym_comment where gid = ? order by ctime limit ?,?;";
            commentList = queryRunner.query(DruidUtils.getConnection(),sql,new BeanListHandler<Comment>(Comment.class),gid,(pageNo-1)*pageSize,pageSize);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return commentList;
    }

    /**
     * 查询商品评论数
     *
     * @param gid
     * @return
     */
    @Override
    public long getCountByGid(Integer gid) throws SQLException {
        long count = 0;
        try {
            String sql = "select count(0) as count from ym_comment where gid = ?;";
            count = queryRunner.query(DruidUtils.getConnection(),sql,new ScalarHandler<Long>(),gid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    /**
     * 通过用户查询评论
     *
     * @param uid
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public List<Comment> getCommentByUid(Integer uid, Integer pageNo, Integer pageSize) throws SQLException {
        List<Comment> commentList = new ArrayList<>();
        try {
            String sql = "select * from ym_comment where uid = ? order by ctime limit ?,?;";
            commentList = queryRunner.query(DruidUtils.getConnection(),sql,new BeanListHandler<Comment>(Comment.class),uid,(pageNo-1)*pageSize,pageSize);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return commentList;
    }

    /**
     * 查询用户评论数
     *
     * @param uid
     * @return
     */
    @Override
    public long getCountByUid(Integer uid) throws SQLException {
        long count = 0;
        try {
            String sql = "select count(0) as count from ym_comment where uid = ?;";
            count = queryRunner.query(DruidUtils.getConnection(),sql,new ScalarHandler<Long>(),uid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    /**
     * 添加评论
     *
     * @param comment
     * @return
     */
    @Override
    public int add(Comment comment) throws SQLException {
        int num = 0;
        try {
            String sql = "insert into ym_comment(cid, uid, gid, ccontent, ctime) value (?,?,?,?,?);";
            num = queryRunner.update(DruidUtils.getConnection(),sql,comment.getCid(),comment.getUid(),comment.getGid(),comment.getCcontent(),comment.getCtime());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return num;
    }

    /**
     * 删除评论
     *
     * @param cid
     * @return
     */
    @Override
    public int delete(Integer cid) throws SQLException {
        int num = 0;
        try {
            String sql = "delete from ym_comment where cid = ?;";
            num = queryRunner.update(DruidUtils.getConnection(),sql,cid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return num;
    }

    /**
     * 删除商品所有评论
     *
     * @param gid
     * @return
     * @throws SQLException
     */
    @Override
    public int deleteByGid(Integer gid) throws SQLException {
        int num = 0;
        try {
            String sql = "delete from ym_comment where gid = ?;";
            num = queryRunner.update(DruidUtils.getConnection(),sql,gid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return num;
    }
}
